package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.SequenceCriteria;
import com.leo.ibatis.demo.domain.Sequence;
import com.leo.ibatis.demo.service.SequenceService;

public class SequenceController extends MultiActionController {

	private SequenceService sequenceService;

	public void setSequenceService(SequenceService sequenceService) {
		this.sequenceService = sequenceService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SequenceCriteria sequenceCriteria = new SequenceCriteria();
		bind(request, sequenceCriteria);
		
		List<Sequence> sequenceList = sequenceService.listOnPage(sequenceCriteria);
		
		ModelAndView mav = new ModelAndView("sequence/list");
		mav.addObject("sequenceList", sequenceList);
		mav.addObject("sequenceCriteria", sequenceCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SequenceCriteria sequenceCriteria = new SequenceCriteria();
		bind(request, sequenceCriteria);
			
		String name = ServletRequestUtils.getRequiredStringParameter(request, "name");

		Sequence sequence = sequenceService.select(name);
		
		ModelAndView mav = new ModelAndView("sequence/detail");
		mav.addObject("sequence", sequence);
		mav.addObject("sequenceCriteria", sequenceCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name = ServletRequestUtils.getRequiredStringParameter(request, "name");

		sequenceService.delete(name);
		
		return new ModelAndView("redirect:sequence.do");
	}

}
